package cn.sylinx.hbatis.ext.proxy.invoker;

import java.util.Map;

import cn.sylinx.hbatis.ext.parse.SqlParser;
import cn.sylinx.hbatis.ext.starter.util.DaoHelper;
import cn.sylinx.hbatis.kit.Tuple;

/**
 * 返回Bean对象页面
 * 
 * @author han
 *
 */
public class BeanPageCommandInvoker extends AbstractPageCommandInvoker {

	private String nativeSql;

	public BeanPageCommandInvoker(String truelyDatasource, String sql, Map<String, Object> params, Integer pageNumber,
			Integer pageSize, Class<?> truelyReturnType, String nativeSql) {
		super(truelyDatasource, sql, params, pageNumber, pageSize, truelyReturnType);
		this.nativeSql = nativeSql;
	}

	@Override
	public Object invoke() {

		// 使用native sql
		boolean isNativeSql = nativeSql != null && !"".equals(nativeSql.trim());

		if (isNativeSql) {

			Tuple t = SqlParser.parseSql(nativeSql, params, null);
			String truelyNativeSql = t.getObject(0);
			Object[] truelyParams = t.getObject(1);
			
			return DaoHelper.create(truelyDatasource).queryPage(truelyNativeSql, pageNumber, pageSize, truelyReturnType, truelyParams);
		}

		return DaoHelper.create(truelyDatasource).queryPage(sql, pageNumber, pageSize, params, truelyReturnType);
	}

}
